<?php

namespace App\Console\Commands;

use App\Console\Commands\Common\CommonCommand;
use App\Imports\LngLogImport;
use App\Models\Flight\FlightInfo;
use App\Models\Flight\Order;
use App\Models\Rbac\Permission;
use App\Models\User\User;

use Barryvdh\Snappy\Facades\SnappyPdf as PDF;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Kkokk\Poster\PosterManager;
use Maatwebsite\Excel\Facades\Excel;

class Demo extends CommonCommand {


    public $signature = 'demo';
    private $instance;



    public function handle(){

        dd(md5('123456'));


    }

    public function qrCode($code){

        $orders = Order::where(['code'=>$code])->where('is_hide',0)->get();
        $flight = FlightInfo::query()->where('id',$orders->first()->flight_id)->first();
        $coupleFlight = FlightInfo::query()->where('id',$orders->first()->couple_flight_id)->first();


        $strContent = $orders->first()->code." ";
        foreach ($orders as $order){
            $gender = $order->passenger_sex=='M'?"Mr":"Miss";
            $strContent .= "$gender $order->name";
        }

        $strContent.="$flight->flight_no $coupleFlight->flight_no";
        return $strContent;
    }

    public function toPDF()
    {

        $code = 'MKRAGO';
        $records = Order::query()->where(['code'=>$code,'is_hide'=>0])->with('getFlightInfo')->get();
        $firstRecord = $records->first();
        $createdAt = Carbon::parse(date('Y-m-d',strtotime($firstRecord->created_at)))->format('D j F Y');
        $isDouble = $firstRecord->is_double;
        $flightInfos = [];
        $passengers = [];
        $extra = [];
        $flight = FlightInfo::query()->where('id',$firstRecord->flight_id)->first();
        $during = Carbon::parse($flight->start_fly_time)->diffInMinutes(Carbon::parse($flight->end_fly_time));
        //=============================== 航班 ===========================================================
        $flightInfos[] = [
            'date'=>Carbon::parse($flight->date)->format('l j F Y'),
            'flight_no'=>$flight->flight_no,
            'simple_start_airport'=>$flight->start_airport,
            'simple_end_airport'=>$flight->end_airport,
            'start_airport'=>getAirport($flight->start_airport),
            'end_airport'=>getAirport($flight->end_airport),
            'start_fly_time'=>$flight->start_fly_time,
            'end_fly_time'=>$flight->end_fly_time,
            'during'=>toHumanHours($during)
        ];
        //往返航班
        if($isDouble==1){
            $coupleFlight = FlightInfo::query()->where('id',$firstRecord->couple_flight_id)->first();
            $during = Carbon::parse($coupleFlight->start_fly_time)->diffInMinutes(Carbon::parse($coupleFlight->end_fly_time));
            $flightInfos[] = [
                'date'=>Carbon::parse($coupleFlight->date)->format('l j F Y'),
                'flight_no'=>$coupleFlight->flight_no,
                'simple_start_airport'=>$coupleFlight->start_airport,
                'simple_end_airport'=>$coupleFlight->end_airport,
                'start_airport'=>getAirport($coupleFlight->start_airport),
                'end_airport'=>getAirport($coupleFlight->end_airport),
                'start_fly_time'=>$coupleFlight->start_fly_time,
                'end_fly_time'=>$coupleFlight->end_fly_time,
                'during'=>toHumanHours($during)
            ];
        }
        //=============================== 乘客 ===========================================================
        foreach ($records as $record) {
            $passengers[] = [
                'sub_name'=>getSubName($record->name),
                'name'=>implode(' ', array_reverse(explode(' ', $record->name))),
                'type'=>getPassengerTypeForEnglish($record->passenger_type)
            ];
        }
        //=============================== Extra services ===========================================================

        $pdf = PDF::loadView('order',compact('firstRecord','isDouble','createdAt','flightInfos','passengers')); // welcome 为html 页面
        $pdf->setOption('encoding', 'utf-8');
        //$pdf->setOption('encoding', 'gb18030');
        $pdf->setOption('enable-local-file-access', true); // welcome 页面引入本地静态资源后使用，否则生成pdf时test引入本地静态资源失败（例如：本地字体）
        $fileName = $record->first()->name.'-'.uniqid();
        if(empty($fileName))$fileName=Str::random(10);
        //return $pdf->download($fileName.'.pdf');
        $filePath = storage_path(time() . '.pdf');
        $pdf->setPaper('a4')->save($filePath);
        return 0;
    }


    public function toPDF1()
    {

        $code = 'MKRAGO';
        $records = Order::query()->where(['code'=>$code,'is_hide'=>0])->with('getFlightInfo')->get();
        $firstRecord = $records->first();
        $createdAt = Carbon::parse(date('Y-m-d',strtotime($firstRecord->created_at)))->format('D ,j F Y');
        $isDouble = $firstRecord->is_double;
        $flightInfos = [];
        $passengers = [];
        $extra = [];
        $flight = FlightInfo::query()->where('id',$firstRecord->flight_id)->first();
        $during = Carbon::parse($flight->start_fly_time)->diffInMinutes(Carbon::parse($flight->end_fly_time));
        //=============================== 航班 ===========================================================
        $flightInfos[] = [
            'date'=>Carbon::parse($flight->date)->format('l ,j F Y'),
            'flight_no'=>$flight->flight_no,
            'simple_start_airport'=>$flight->start_airport,
            'simple_end_airport'=>$flight->end_airport,
            'start_airport'=>getAirport($flight->start_airport),
            'end_airport'=>getAirport($flight->end_airport),
            'start_fly_time'=>$flight->start_fly_time,
            'end_fly_time'=>$flight->end_fly_time,
            'during'=>toHumanHours($during)
        ];
        //往返航班
        if($isDouble==1){
            $coupleFlight = FlightInfo::query()->where('id',$firstRecord->couple_flight_id)->first();
            $during = Carbon::parse($coupleFlight->start_fly_time)->diffInMinutes(Carbon::parse($coupleFlight->end_fly_time));
            $flightInfos[] = [
                'date'=>Carbon::parse($coupleFlight->date)->format('l ,j F Y'),
                'flight_no'=>$coupleFlight->flight_no,
                'simple_start_airport'=>$coupleFlight->start_airport,
                'simple_end_airport'=>$coupleFlight->end_airport,
                'start_airport'=>getAirport($coupleFlight->start_airport),
                'end_airport'=>getAirport($coupleFlight->end_airport),
                'start_fly_time'=>$coupleFlight->start_fly_time,
                'end_fly_time'=>$coupleFlight->end_fly_time,
                'during'=>toHumanHours($during)
            ];
        }
        //=============================== 乘客 ===========================================================
        foreach ($records as $record) {
            $passengers[] = [
                'sub_name'=>getSubName($record->name),
                'name'=>$record->name,
                'type'=>getPassengerTypeForEnglish($record->passenger_type)
            ];
        }
        //=============================== Extra services ===========================================================

        $pdf = PDF::loadView('order',compact('isDouble','createdAt','flightInfos','passengers')); // welcome 为html 页面
        $pdf->setOption('encoding', 'utf-8');
        //$pdf->setOption('encoding', 'gb18030');
        $pdf->setOption('enable-local-file-access', true); // welcome 页面引入本地静态资源后使用，否则生成pdf时test引入本地静态资源失败（例如：本地字体）
        $fileName = $record->first()->name.'-'.uniqid();
        if(empty($fileName))$fileName=Str::random(10);
        //return $pdf->download($fileName.'.pdf');
        $filePath = storage_path(time() . '.pdf');
        $pdf->setPaper('a4')->save($filePath);
        return 0;
    }

    public function pdf(){
        $id = 10;

        $record = FlightInfo::find($id)->toArray();
        $HD_EN = base64_encode(file_get_contents(public_path('img/HD_EN.jpg')));
        $pdf417 = base64_encode(file_get_contents(public_path('img/pdf417.png')));
        $VJ_HDBank_EN = base64_encode(file_get_contents(public_path('img/VJ-HDBank-EN.png')));
        $petrologo = base64_encode(file_get_contents(public_path('img/petrologo.jpg')));
        $vietjetair = base64_encode(file_get_contents(public_path('img/vietjetair.png')));
        $SkyJoy_EN = base64_encode(file_get_contents(public_path('img/SkyJoy_EN.jpg')));

        $pdf = PDF::loadView('1', compact('HD_EN', 'pdf417', 'VJ_HDBank_EN', 'petrologo', 'vietjetair', 'SkyJoy_EN', 'record')); // welcome 为html 页面
        $pdf->setOption('encoding', 'utf-8');
        $pdf->setOption('enable-local-file-access', true); // welcome 页面引入本地静态资源后使用，否则生成pdf时test引入本地静态资源失败（例如：本地字体）
        $filePath = storage_path(time() . '.pdf');
        $pdf->setPaper('a4')->save($filePath);
        return 0;
    }
}
